﻿using System;
using System.Collections;
using System.ComponentModel.Design;
using System.Linq;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Behaviors
{
    public class InheritedControlCommandDesignerBehavior : DesignerBehavior
    {
        private static readonly Guid selectionMenuGroup = new Guid("{74D21312-2AEE-11d1-8BFB-00A0C90F26F7}");
        private static readonly Guid defaultKey97Group = new Guid("{5EFC7975-14BC-11CF-9B2B-00AA00573819}");

        public static readonly CommandID SelectionMenu = new CommandID(selectionMenuGroup, 0x500);
        public static readonly CommandID CopyCommandId = new CommandID(defaultKey97Group, 0xF);
        public static readonly CommandID CutCommandId = new CommandID(defaultKey97Group, 0x10);
        public static readonly CommandID PasteCommandId = new CommandID(defaultKey97Group, 0x1A);
        public static readonly CommandID DeleteCommandId = new CommandID(defaultKey97Group, 0x11);

        /// <summary>
        /// Initializes a new instance of the <see cref="InheritedControlCommandDesignerBehavior"/> class.
        /// </summary>
        /// <param name="designerHost">The designer host.</param>
        public InheritedControlCommandDesignerBehavior(IDesignerHost designerHost) 
            : base(designerHost)
        {
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public override void Initialize()
        {
            this.GetService<ISelectionService>().SelectionChanged += OnSelectionChanged;
        }

        /// <summary>
        /// Called when [selection changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OnSelectionChanged(object sender, EventArgs e)
        {
            ICollection selectedComponents = this.GetService<ISelectionService>().GetSelectedComponents();

            bool canDeleteAndCut = true;

            foreach (var frameXmlControl in selectedComponents.OfType<IFrameXmlControl>())
            {
                if (frameXmlControl.IsInherited)
                {
                    canDeleteAndCut = false;
                    break;
                }
            }

            var menuCommandService = this.GetService<IMenuCommandService>();
            if (menuCommandService != null)
            {
                var cutCommand = menuCommandService.FindCommand(CutCommandId);
                if (cutCommand != null)
                    cutCommand.Supported = canDeleteAndCut;

                var deleteCommand = menuCommandService.FindCommand(DeleteCommandId);
                if(deleteCommand != null)
                    deleteCommand.Supported = canDeleteAndCut;
            }
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public override void Dispose()
        {
            var selectionService = this.GetService<ISelectionService>();
            if (selectionService != null)
                selectionService.SelectionChanged -= OnSelectionChanged;
        }
    }
}

